﻿using System.ComponentModel;
using System.Windows.Controls;
using MVVMtutorialAdvanced.DomainServices;
using MVVMtutorialAdvanced.Web;
using Riba.MVVMSL;

namespace MVVMtutorialAdvanced
{
	public class Author_VM : ViewModelBase
	{
		public RelayCommand CommandEdit { get; private set; }
		public RelayCommand CommandInsert { get; private set; }
		public RelayCommand CommandDelete { get; private set; }

		public DomainDataSource DomainDataSourceAuthors
		{ get; private set; }

		private readonly IDomainServiceBook _domainService;

		public Author_VM(IDomainServiceBook domainService)
		{
			DomainDataSourceAuthors = new DomainDataSource();
			domainService.SetDomainDataSourceAuthors(DomainDataSourceAuthors);
			DomainDataSourceAuthors.SortDescriptors.Add(new SortDescriptor("LastName", ListSortDirection.Ascending));

			_domainService = domainService;

			CommandEdit = new RelayCommand
				(
					commandData =>
						{
							if (DomainDataSourceAuthors.DataView.CurrentItem == null)
							  return;

							var args = new LoadChildWindowEventArgs(
								typeof(AuthorDetails_UC), DomainDataSourceAuthors.DataView.CurrentItem, CallbackFromChildWindow);

							RaiseLoadChildWindow(args);
						}
				);

			CommandInsert = new RelayCommand
				(
					commandData =>
						{
							DomainDataSourceAuthors.DataView.Add(new Author());

							var args = new LoadChildWindowEventArgs(
									typeof(AuthorDetails_UC), DomainDataSourceAuthors.DataView.CurrentItem, CallbackFromChildWindow);

							RaiseLoadChildWindow(args);
						}
				);

			CommandDelete = new RelayCommand
				(
					commandData =>
						{
							if (DomainDataSourceAuthors.DataView.CurrentItem == null)
								return;

							IsBusy = true;
							DomainDataSourceAuthors.DataView.Remove(DomainDataSourceAuthors.DataView.CurrentItem);
							DomainDataSourceAuthors.SubmittedChanges += DomainDataSourceAuthors_SubmittedChanges;
							DomainDataSourceAuthors.SubmitChanges();
						}
				);

			GetItems();
		}

		private void GetItems()
		{
			//DomainDataSourceAuthors.IsBusy could be used instead of VM.IsBusy

			IsBusy = true;

			_domainService.GetAuthors
				(
					error =>
						{
							IsBusy = false;

							if (error != null)
							{
								RaiseShowMsgBox(new ShowMsgBoxEventArgs(error.Message, "Fehler in GetItems"));
								return;
							}

							CommandEdit.IsEnabled = true;
							CommandInsert.IsEnabled = true;
							CommandDelete.IsEnabled = true;
						}
				);
		}

		private void CallbackFromChildWindow(ViewModelBase sender, object data)
		{
			if ((bool)data == false)
				DomainDataSourceAuthors.RejectChanges();
			else
			{
				IsBusy = true;

				DomainDataSourceAuthors.SubmittedChanges += DomainDataSourceAuthors_SubmittedChanges;
				DomainDataSourceAuthors.SubmitChanges();
			}
		}

		void DomainDataSourceAuthors_SubmittedChanges(object sender, SubmittedChangesEventArgs e)
		{
			IsBusy = false;

			if (e.HasError)
			{
				RaiseShowMsgBox(new ShowMsgBoxEventArgs(e.Error.Message, "Fehler in DomainDataSourceAuthors_SubmittedChanges"));
				return;
			}
		}
	}
}
